package com.sgj.watchinyou.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;

import com.google.android.gms.maps.model.LatLng;

public class LocationUtils extends Activity{
	
	public static String getAddressFromLocaion(Context context, Location location) {
		List<Address> locationName = new ArrayList<Address>();
		Geocoder geocoder = new Geocoder(context, Locale.getDefault());
		String countryName = null;
		
		if(Geocoder.isPresent()){	
			try {
				locationName = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if(!locationName.isEmpty()){
			String locality = locationName.get(0).getLocality() != null ? locationName.get(0).getLocality() + " " : "";
			String subLocality = locationName.get(0).getSubLocality() != null ? locationName.get(0).getSubLocality() + " " : "";
			String throughfare = locationName.get(0).getThoroughfare() != null ? locationName.get(0).getThoroughfare() + " " : "";
			String subThroughfare = locationName.get(0).getSubThoroughfare() != null ? locationName.get(0).getSubThoroughfare() + " " : "";
			countryName = locality + subLocality + throughfare + subThroughfare;			
		}
		return countryName;
	}
	
	public static LatLng getLatLng(Location location){
		return new LatLng(location.getLatitude(), location.getLongitude());
	}
}
